#include "stdafx.h"
#include "Quick_reply_op.h"
#include <string>
#include <Util/StringUtil.h>
#include <Util/CodeUtil.h>
#include <Util/StringUtil.h>
#include <util/WinAPITools.h>
#include <interface/log_interface.h>
#include <interface/data_op/table_wrapper.h>

namespace aot{ namespace data{

	QuickReply::QuickReply()
	{

	}

	QuickReply::~QuickReply()
	{

	}

	bool QuickReply::get_all(aot::tt::ivec_str** out)
	{
		aot::tt::vec_str_impl* p = new aot::tt::vec_str_impl();
		*out = (aot::tt::ivec_str*)p;

		std::string strSql;
		strSql = "select * from quick_reply;";
		aot::data::table_helper _table;

		bool bRet = g_db_user->exec_and_get_record(strSql.c_str(), _table);

		std::string strValue;
		for( int i = 0; i < _table.get_rows_count(); i++ )
		{
			_table.set_row_index(i);
			strValue = _table.get_field_value_str("content");
			p->push_back(strValue.c_str());
		}
		return bRet;
	}

	bool QuickReply::update(aot::tt::ivec_str* p)
	{
		if( NULL == p )
			return false;

		std::string strSql;
		strSql = "delete from quick_reply;";
		bool ret = g_db_user->exec(strSql.c_str()); 

		strSql = "insert into quick_reply (ID,content) values('%d','%s');";
		int i = 1;
		for ( i = 1, p->goto_begin(); !p->is_end(); p->goto_next(), ++i )
		{
			std::string reply_info = p->get_value();
			std::string strInsert = aot::tt::utility::StrUtil::Printf(strSql.c_str(), i, reply_info.c_str());
			if(reply_info == "")
			{
				continue;
			}
			ret = g_db_user->exec(strInsert.c_str());
			if (ret == false)
			{
				return false;
			}
		}
	}

}}